<template>
	<div class="container add_edit">
		<el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
			<el-form-item label="会员名称" ref="name">
				<el-input v-model="form.name" placeholder="会员名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="开通方式">
				<el-radio-group v-model="form.way">
					<el-radio label="购买">购买</el-radio>
					<el-radio label="积分兑换">积分兑换</el-radio>
					<el-radio label="储值">储值</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item v-if="form.way==='购买'" label="金额" ref="name">
				<el-input v-model="form.money" placeholder="购买金额" clearable type="number"></el-input>
			</el-form-item>
			<el-form-item v-if="form.way==='积分兑换'" label="积分" ref="name">
				<el-input v-model="form.integral" placeholder="积分" clearable type="number"></el-input>
			</el-form-item>
			<el-form-item v-if="form.way==='储值'" label="储值" ref="name">
				<el-input v-model="form.recharge_money" placeholder="储值金额" clearable type="number"></el-input>
			</el-form-item>
			<el-form-item v-if="form.way==='储值'" label="到账金额" ref="name">
				<el-input v-model="form.arrival_money" placeholder="到账金额" clearable type="number"></el-input>
			</el-form-item>
			<el-form-item label="会员权益">
				<el-checkbox v-model.number="form.vip_price" :true-label="true" :false-label="false">会员价</el-checkbox>
				<el-checkbox v-model.number="form.double_integral" :true-label="true"
					:false-label="false">双倍积分</el-checkbox>
				<el-checkbox v-model.number="form.partner" :true-label="true" :false-label="false">分销合伙人</el-checkbox>
			</el-form-item>
			<el-form-item v-if="form.vip_price" label="会员折扣" ref="name">
				<el-input v-model="form.vip_price_discount" placeholder="会员折扣(例:0.95折)" clearable
					type="number"></el-input>
			</el-form-item>
			<h3>VIP权益说明</h3>
			<el-form-item label-width="120px">
				<use-editor class="margin_t_20" ref="editorVipDesc" @editorChange="editorChangeVipDesc">
				</use-editor>
			</el-form-item>
			<el-form-item>
				<el-button :loading="formLoading" round icon="el-icon-check" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
				<el-button round icon="el-icon-back" class="return-btn" @click="returnPage">返回</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'wmzmall-member-config';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					name: '',
					way: '购买',
					money: "0",
					integral: "0",
					recharge_money: "0",
					arrival_money: "0",
					vip_desc: '',
					vip_price: false,
					double_integral: false,
					partner: false,
					vip_price_discount: ""
				},
				rules: {}
			};
		},
		methods: {
			async loadData() {
				if (!this.dataId) {
					this.$db[__name].getsort().then(res => {
						if (res.code == 200) {
							this.form.sort = ++res.datas.sort;
						}
					});
					return;
				}

				await this.$db[__name].tofirst(this.dataId).then(res => {
					if (res.code == 200) {
						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}
						if (res.datas.vip_desc) {
							this.form.vip_desc = res.datas.vip_desc;
							this.$refs.editorVipDesc.setContent(this.form.vip_desc);
						}
					}
				});
			},
			editorChangeVipDesc(res) {
				this.form.vip_desc = res;
			},
			submitForm(formName) {
				this.formLoading = true;
				console.log(this.form)
				if (!this.dataId) {
					this.$db[__name].add(this.form).then(res => {
						if (res.code == 200) {
							this.getOpenerEventChannel().emit('loadData');
							this.returnPage();
						}
						this.formLoading = false;
					}).catch(() => {
						this.formLoading = false;
					})
				} else {
					this.$db[__name].update(this.dataId, this.form).then(res => {
						if (res.code == 200) {
							this.getOpenerEventChannel().emit('loadData');
							this.returnPage();
						}
						this.formLoading = false;
					}).catch(() => {
						this.formLoading = false;
					})
				}
			},
			returnPage() {
				uni.$emit('closeTab');
				uni.redirectTo({
					url: '/pages/user/member_set'
				});
			}
		},
		created() {
			this.dataId = this.$route.query.id || '';
			this.loadData();
		}
	};
</script>

<style></style>